﻿using StudentCouncil.Model;
using StudentCouncil.Model.Models;
using StudentCouncil.Model.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentCouncil.DAL.DAO
{
    public class MessageDAO
    {
        public static bool UpdateInfo(Message entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.Message.Find(entity.MessageId);
                if (null == dbEntity)
                {
                    //Log4Helper.InfoLog.Warn("未找到该user id：" + entity.ID);
                    return false;
                }
                else
                {
                    dbEntity.Messages = entity.Messages;
                    dbEntity.MeVideo = entity.MeVideo;
                    db.SaveChanges();
                    return true;
                }
            }
        }

        public static bool AddMessage(Message entity)
        {
            using (StudentContext db = new StudentContext())
            {
                db.Message.Add(entity);
                db.SaveChanges();
                return true;
            }
        }
        public static Pager<Message> GetListByPage(MessageParameter parameter)
        {
            Pager<Message> result = new Pager<Message>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.Message.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Messages))
                {
                    query = query.Where(t => t.Messages.Contains(parameter.Messages));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.Messages)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }
        public static bool DeleteById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var entity = db.Message.Find(id);
                if (null != entity)
                {
                    db.Message.Remove(entity);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public static bool BatchDelete(string[] ids)
        {
            using (StudentContext db = new StudentContext())
            {
                var list = db.Message.Where(t => ids.Contains(t.MessageId.ToString()));
                db.Message.RemoveRange(list);
                db.SaveChanges();
                return true;
            }
        }

        public static bool UploadMV(Message entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.Message.Find(entity.MessageId);
                dbEntity.MeVideo = entity.MeVideo;

                db.SaveChanges();
                return true;
            }
        }
    }
}
